let layer = layui.layer;
// 点击跳转  修改用户界面
$('.user-icon').on('click', function () {
  location.href = '/user-edit.html';
});

getData();
// 获取用户数据
function getData() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/user/getUserInfo',
    headers: {
      token: localStorage.getItem('token'),
    },
  }).then(function (res) {
    console.log(res);
    if (res.data.errno !== 0) {
      return;
    }
    // console.log(res.data.data);
    renderAvatar(res.data.data.userInfo);
    let htmlStr = `<a href="user-add.html" class="button">
                        <i class="fa fa-plus"></i>
                        立即发布文章
                      </a>
                      <a href="user-collect.html" class="button">
                        <i class="fa fa-heart"></i>
                        收藏了
                        <b>${res.data.data.collectNum}</b>
                        篇文章
                      </a>
                      <a href="user-zan.html" class="button">
                        <i class="fa fa-thumbs-up"></i>
                        点赞了
                        <b>${res.data.data.zanNum}</b>
                        篇文章
                      </a>
                      <a href="user-list.html" class="button">
                        <i class="fa fa-tasks"></i>
                        发布了
                        <b>${res.data.data.articleNum}</b>
                        篇文章
                      </a>`;
    $('.btn-box').html(htmlStr);
  });

  // 渲染信息
  function renderAvatar(user) {
    // console.log(user);
    if (user.icon == null) {
      $('.user-icon').attr('src', './lib/2.jpg');
    } else {
      $('.user-icon').attr('src', user.icon);
    }
    // 判断 昵称、宣言、生日为空   性别0 1
    if (user.name == null) {
      user.name = '仲夏夜';
    }
    if (user.birthday == null) {
      user.birthday = '2002-02-06';
    }
    if (user.notes == null) {
      user.notes = '~';
    }
    var sex = user.sex == 0 ? '男' : '女';

    $('.user-info h3 span').html('昵称:&nbsp;&nbsp;' + user.name);
    $('.user-info h3 b').html('个性宣言:&nbsp;' + user.notes);

    // console.log($('.user-info').find('p span').eq(0).html());
    $('.user-info')
      .find('p span')
      .eq(0)
      .html('性别:' + sex);
    $('.user-info')
      .find('p span')
      .eq(1)
      .html('手机号:' + user.mobile);
    $('.user-info')
      .find('p span')
      .eq(2)
      .html('出生年月:' + user.birthday);
  }
}
// 首页和全局数据
function homePage() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/index/index',
  }).then(function (res) {
    console.log(res);
    // 导航栏
    let strNav = res.data.data.allCate
      .map(item => {
        return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
      })
      .join('');
    $('#nav').append(strNav);
  });
}
userCenter();
homePage();
// 用户中心和退出
function userCenter() {
  // 没有token的时候隐藏用户中心和退出
  if (localStorage.getItem('token')) {
    let str = `
         <a href="user.html" id="user"><i class="fa fa-user"></i>&nbsp;用户中心</a>
         <a href="javascript:;" class="quit"><i></i>&nbsp;退出</a>`;
    $('.slinks').html(str);
  } else {
    let str = `
          <a href="login.html"><i class="fa fa-hand-o-right"></i>&nbsp;登录</a> |
          <a href="reg.html"><i class="fa fa-registered"></i>&nbsp;注册</a>`;
    $('.slinks').html(str);
  }
  $('#user').on('click', function (e) {
    e.preventDefault();
    if (localStorage.getItem('token')) {
      location.href = 'user.html';
    } else {
      location.href = 'login.html';
    }
  });
  $('.quit').on('click', function (e) {
    layui.layer.confirm('你确定要退出吗?', { icon: 3, title: '退出提示' }, function (index) {
      localStorage.removeItem('token');
      location.reload();
      layui.layer.close(index);
    });
  });
}
// 导航栏跳转
function getNavSkip() {
  $('#nav').on('click', 'a', function (e) {
    e.preventDefault();
    window.open('../list.html?id=' + $(this).attr('id'));
  });
}
getNavSkip();
